<?php
/*
 * @Date: 2021-01-29 22:52:45
 * @名称: 用户 - 用户登陆 - 验证用户
 * @版本: 0.01
 * @作者: 初雪桜
 * @邮箱: 202184199@qq.com
 * @最后编辑: 初雪桜
 * @LastEditTime: 2021-03-01 21:53:37
 * @FilePath: /Site/app/useradmin/controller/User/Login/VerificationUser.php
 */

namespace app\useradmin\controller\User\Login;

use app\BaseController;
use think\facade\Db;

class VerificationUser extends BaseController
{
    public function Run($userName, $passWord)
    {

        /*
        $userName = "sakura_223@icloud.com"; //登录账号
        $passWord = "12345678A"; //登录密码 
        $userName = "202184199@qq.com"; //登录账号
        $passWord = "misakimei123A"; //登录密码*/
        $platform = "web"; //登录模式 web模式
        $userState = Db::table('user_info')
            ->where([
                "user_name" => $userName,
                "pass_word" => $passWord,
            ])
            ->select();
        if (count($userState) > 1) { //如果查找到多个用户
            return json_decode(errorJsonReturn("User-10000")->getContent(), true);
        }
        if (count($userState) < 1) { //如果没有用户
            return json_decode(errorJsonReturn("User-10001")->getContent(), true);
        }
        $token = json_encode([
            "_id" => $userState[0]['_id'], //用户id
            "email" => $userName, //邮箱
            "role" => $userState[0]['role'], //身份
            "name" => $userState[0]['name'], //昵称
            "platform" => $platform, //系统版本 ios/android/web
            "iat" => time(), //申请时间
            "exp" => time() + 604800, //过期时间
        ]);
        $EncryptionMethod = json_encode([ //加密方式
            "Encryption" => "SHA256", //加密方法 假的误导用 
            "verification" => "SHA512",
        ]);
        $str1 = base64_encode($EncryptionMethod); //头部验证信息
        $str2 = base64_encode(authcode($token, 'encode', config('serverConfig.TokenResolutionKey'))); //加密字符串
        $str3 = hash_hmac("sha512", $str2, config('serverConfig.TokenResolutionKeySha512')); //取 加密字符串的 sha512
        $data["message"] = "success";
        $data["data"]["token"] = "$str1.$str2.$str3";
        $data["code"] = "200";
        cookie("Authorization", "$str1.$str2.$str3");
        session("IsWebAdmin", $userState[0]["is_web_admin"]); //是否为网站管理员
        session("HeadImages", $userState[0]['file_server'] . $userState[0]['path']);
        Db::table('user_info')
            ->where($userState[0])
            ->update([
                "backstage_to_ken" => "$str1.$str2.$str3"
            ]);
        return json_decode(successJsonReturn(["Authorization" => "$str1.$str2.$str3"])->getContent(), true);
    }
}
